package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AdditionalUserCommodityService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.AdditionalCommodityDao;
import com.qdlc.p2p.dal.dao.AdditionalUserCommodityDao;
import com.qdlc.p2p.dal.dto.AdditionalUserCommodity;
import com.qdlc.p2p.dal.model.AdditionalUserCommodityModel;

/**
 * Created by liaoxiangkai on 2015/12/9.
 */
@Service("additionalUserCommodityService")
public class AdditionalUserCommodityServiceImpl implements AdditionalUserCommodityService {

    @Resource
    private AdditionalUserCommodityDao additionalUserCommodityDao;

    @Resource
    private AdditionalCommodityDao additionalCommodityDao;

    /**
     * 用户获得商品列表
     * @param model
     * @return
     */
    @Override
    public PageDataList<AdditionalUserCommodityModel> list(AdditionalUserCommodityModel model) {
        return additionalUserCommodityDao.list(model);
    }

    /**
     * 通过id查询
     * @param model
     * @return
     */
    @Override
    public AdditionalUserCommodityModel findById(AdditionalUserCommodityModel model) {
        AdditionalUserCommodity additionalUserCommodity = additionalUserCommodityDao.find(model.getId());
        AdditionalUserCommodityModel additionalUserCommodityModel = AdditionalUserCommodityModel.instance(additionalUserCommodity);
        //设置审核人名字
        if (null != additionalUserCommodity.getAuditOperator())
            additionalUserCommodityModel.setAuditName(additionalUserCommodity.getAuditOperator().getUserName());
        //设置发货人
        if (null != additionalUserCommodity.getPostOperator())
            additionalUserCommodityModel.setPostName(additionalUserCommodity.getPostOperator().getUserName());
        //设置用户名
        if (null == additionalUserCommodity.getUser() || null == additionalUserCommodity.getAdditionalCommodity()) return null;
        additionalUserCommodityModel.setUserName(additionalUserCommodity.getUser().getUserName());
        additionalUserCommodityModel.setRealName(additionalUserCommodity.getUser().getRealName());
        //设置物品名称
        additionalUserCommodityModel.setCommodityName(additionalUserCommodity.getAdditionalCommodity().getName());
        return additionalUserCommodityModel;
    }

    /**
     * 该抽中奖品用户ip对应数据库所有该ip总数
     * @return
     */
    public int SingleIpCount(String addIp){
        return additionalUserCommodityDao.SingleIpCount(addIp);
    }

    /**
     * 审核
     * @param model
     * @return
     */
    @Override
    public int doAudit(AdditionalUserCommodityModel model) {
        return additionalUserCommodityDao.doAudit(model);
    }

    /**
     * 发货操作
     *  --更新商品发行数量
     *  --更新用户商品表状态
     * @param model
     * @return
     */
    @Override
    public int doPost(AdditionalUserCommodityModel model) {
        //TODO 两个更新操作，可能会产生更新不一致
        int userResult = additionalUserCommodityDao.doPost(model);
        int result = 0;
        AdditionalUserCommodity additionalUserCommodity = additionalUserCommodityDao.find(model.getId());
        if (userResult > 0) {
            result = additionalCommodityDao.updateIssueNum(additionalUserCommodity.getAdditionalCommodity().getId());
            if (result <= 0) {
                throw new RuntimeException();
            }
        }
        return result;
    }


}
